-- SPDX-License-Identifier: GPL-3.0-or-later
---@class StoryData: Object
local Battle = class("Battle")
local Map = require "packages/wdtx/Map"
local WU = require "packages/wdtx/WUltiity"
function Battle:initialize(player,data,obj)
  self.player = player
  self.wdtxobj = obj
  self.battledata = data
  self:initBattleData()
end

function Battle:initBattleData()
  local battledata = self:getBattleData()
  self.normonster = table.clone(battledata.normonster)
  self.elitemonster = table.clone(battledata.elitemonster)
  self.boss = table.clone(battledata.boss)

  self.normonskills = table.clone(battledata.norskills)
  self.elitemonskills = table.clone(battledata.eliteskills)
  self.bossskills = table.clone(battledata.bossskills)
end

function Battle:getBattleData()
  return self.battledata[self.wdtxobj.storyid]
end

function Battle:getEliteMonsterData()
  return self.elitemonster
end

function Battle:getNorMonsterData()
  return self.normonster
end

function Battle:getBossData()
  return self.boss
end

function Battle:getNorMonSkillsData()
  return self.normonskills
end

function Battle:getEliteMonSkillsData()
  return self.elitemonskills
end

function Battle:getBossSkillsData()
  return self.bossskills
end


function Battle:setAddancedBattle(genstype,config,deadplayers)
  local room = self.player.room
  local battlenum = config.num
  local bossskillnum = config.bossskillnum
  local eliskillnum = config.eliskillnum
  local norskillnum = config.norskillnum
  local strength = config.strength
  if battlenum and battlenum > 0 and strength and strength > 0 and #deadplayers > 0 then
    --获取武将
    for i =1,math.min(battlenum,#self[genstype]) do
      local deadplayer = table.remove(deadplayers,1)
      local rangen = WU:getRandItemByTB(self[genstype])
      room:changeHero(deadplayer, rangen, true, false, true)
      room:revivePlayer(deadplayer, false)
      room:askForChooseKingdom({deadplayer})
      if deadplayer.role ~= "rebel" then
        room:setPlayerProperty(deadplayer, "role", "rebel")
      end
      room:drawCards(deadplayer, 3+self.wdtxobj.storyid,"#wdtx_rule")
      
      deadplayer.hp = deadplayer.hp + strength
      deadplayer.maxHp = deadplayer.maxHp + strength
      room:setPlayerProperty(deadplayer, "hp", deadplayer.hp)
      room:setPlayerProperty(deadplayer, "maxHp", deadplayer.maxHp)
      deadplayer.shield = strength - 1
      room:broadcastProperty(deadplayer, "shield")

      --获取技能
      local getskills = ""
      if bossskillnum and bossskillnum > 0 then
        for i =1,bossskillnum do
          if #self.bossskills > 0 then
            getskills = getskills..WU:getRandItemByTB(self.bossskills).."|"
          end
        end
      end
      if eliskillnum and eliskillnum > 0 then
        for i =1,eliskillnum do
          if #self.elitemonskills > 0 then
            getskills = getskills..WU:getRandItemByTB(self.elitemonskills).."|"
          end
        end
      end
      if norskillnum and norskillnum > 0 then
        for i =1,norskillnum do
          if #self.normonskills > 0 then
            getskills = getskills..WU:getRandItemByTB(self.normonskills).."|"
          end
        end
      end
      if #getskills > 0 then
        room:handleAddLoseSkills(deadplayer,getskills,nil,false)
      end
    end
  end 
end

function Battle:relifeEnemy(deadplayer,rangen,strength,skills)
  local room = self.player.room
  room:changeHero(deadplayer, rangen, true, false, true)
  room:revivePlayer(deadplayer, false)
  room:askForChooseKingdom({deadplayer})
  if deadplayer.role ~= "rebel" then
    room:setPlayerProperty(deadplayer, "role", "rebel")
  end
  room:drawCards(deadplayer, 3+self.wdtxobj.storyid,"#wdtx_rule")

  if strength > 0 then
    deadplayer.hp = deadplayer.hp + strength
    deadplayer.maxHp = deadplayer.maxHp + strength
    room:setPlayerProperty(deadplayer, "hp", deadplayer.hp)
    room:setPlayerProperty(deadplayer, "maxHp", deadplayer.maxHp)
    deadplayer.shield = strength
    room:broadcastProperty(deadplayer, "shield")
  end

  local getskills = ""
  if strength > 0 and #skills > 0 then
    for i=1,math.min(strength,#skills) do
      getskills = getskills..WU:getRandItemByTB(skills).."|"
    end
    if #getskills > 0 then
      room:handleAddLoseSkills(deadplayer,getskills,nil,false)
    end
  end
end

function Battle:setNorMalBattle()
  local room = self.player.room
  if #self.normonster == 0 then error("没有配置普通敌人将池") return end
  local deadplayers = table.filter(room.players,function(pr) return pr.id < 0 and pr.dead end)
  local percent = self.wdtxobj:getCurProgress()
  local battledata = self:getBattleData()
  local num,strength = 1,1
  for i, area in ipairs(battledata.battlenum) do
    if percent >= area[1] and percent <= area[2] then
      num = i
    end
  end
  for i, area in ipairs(battledata.battlegrow) do
    if percent >= area[1] and percent <= area[2] then
      strength = i
    end
  end
  for i=1,math.min(num,#self.normonster) do
    local deadplayer = table.remove(deadplayers,1)
    local rangen = WU:getRandItemByTB(self.normonster)
    self:relifeEnemy(deadplayer,rangen,strength-1,self.normonskills)
  end
end

function Battle:readyBattle(ty)
  local room = self.player.room
  if ty == Map.EliteMonsterStr then
    local battledata = self:getBattleData()
    local deadplayers = table.filter(room.players,function(pr) return pr.id < 0 and pr.dead end)
    for genspool,config in pairs(battledata.battleeliter) do
      self:setAddancedBattle(genspool,config,deadplayers)
    end
  elseif ty == Map.BossStr then
    local battledata = self:getBattleData()
    local deadplayers = table.filter(room.players,function(pr) return pr.id < 0 and pr.dead end)
    for genspool,config in pairs(battledata.battleboss) do
      self:setAddancedBattle(genspool,config,deadplayers)
    end
  else
    self:setNorMalBattle()
  end
end

function Battle:deployEnemys(data)
  if not data then return end
  local gensconfig = data.gens
  local room = self.player.room
  for _,pr in ipairs(room.players) do
    if pr.id < 0 and pr.dead and #gensconfig > 0 then
      local config = table.remove(gensconfig,1)
      room:changeHero(pr, config.gen, true, false, true)
      room:revivePlayer(pr, false)
      room:askForChooseKingdom({pr})

      local strength = config.strength
      if strength and strength > 0 then
        pr.hp = pr.hp + strength
        pr.maxHp = pr.maxHp + strength
        room:setPlayerProperty(pr, "hp", pr.hp)
        room:setPlayerProperty(pr, "maxHp", pr.maxHp)
        pr.shield = strength
        room:broadcastProperty(pr, "shield")
      end

      local normonskills = config.normonskills
      local getskis = ""
      if normonskills and normonskills > 0 and #self.normonskills > 0 then
        for i=1,math.min(normonskills,#self.normonskills) do
          getskis = getskis..WU:getRandItemByTB(self.normonskills).."|" 
        end
      end

      local eliteskills = config.eliteskills
      if eliteskills and eliteskills > 0 and #self.eliteskills > 0 then
        for i=1,math.min(eliteskills,#self.eliteskills) do
          getskis = getskis..WU:getRandItemByTB(self.eliteskills).."|" 
        end
      end
      if config.extraskills then
        getskis = getskis..table.concat(config.extraskills,"|")
      end
      if #getskis > 0 then
        room:handleAddLoseSkills(pr, getskis, nil, false)
      end
      if config.role then
        room:setPlayerProperty(pr, "role", config.role)
      else
        if pr.role ~= "rebel" then
          room:setPlayerProperty(pr, "role", "rebel")
        end
      end
      if data.talkinfo and data.talkinfo.who == pr.general then
        pr:chat(data.talkinfo.talk)
      end
      room:drawCards(pr, 3 + (strength or 0),"#wdtx_rule")
    end
  end
end

return Battle
